const app = getApp();
let tabs = [{
  id: 1,
  text: '图片',
  status: true,
}, {
  id: 2,
  text: '视频',
}];

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  tabs,
};

var currentPage = {
  data,
  onLoad,
  toggleTab,
  previewImage,
};

var page = Object.assign({}, currentPage);

Page(page)

function onLoad(options) {
  const that = this;
  const id = options && options.id || 1;
  const prevPage = getCurrentPages()[getCurrentPages().length - 2];
  const xbody = prevPage.data.xbody;

  for (let i = 0; i < tabs.length; i++) {
    if (tabs[i].id == id) {
      tabs[i].status = true;
    } else {
      tabs[i].status = false;
    }
  }

  that.setData({
    tabs,
    xbody,
    tabId:id,
  })
}

function toggleTab(event) {
  const that = this;
  var id = event.detail.id;

  that.setData({
    tabId: id,
  })
}

function previewImage(event){
  const that = this;
  const index = event.currentTarget.dataset.index;
  const xbody = that.data.xbody;
  const images =xbody.images;
  let urls = [];

  images.forEach((item)=>{
    urls.push(item.file);
  })

  wx.previewImage({
    urls: urls,
    current:urls[index]
  })

}